package com.jx.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jx.dao.CatDao;
import com.jx.daoimpl.CatDaoImpl;
import com.jx.model.Category;
import com.jx.service.CatService;
import com.jx.servicrimpl.CatServiceImpl;

public class UpdCatController extends HttpServlet{
	//定义CatService引用指向CatServiceImpl对象，以调用service层方法
	CatService updcatservice = new CatServiceImpl();
	CatDao catdao = new CatDaoImpl();
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			// TODO Auto-generated method stub
			doPost(req, resp);
		}
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
			//设置字符集
			req.setCharacterEncoding("UTF8");
			resp.setCharacterEncoding("UTF8");
			//创建流对象
			PrintWriter pw = resp.getWriter();
			//接收从前台传来的数据
			String cname = req.getParameter("cname");
			if (cname=="") {
				pw.write("0");
			}else{
				if (catdao.findCatByCname(cname)) {
					pw.write("existed");
				}else {
					int cid = Integer.parseInt(req.getParameter("cid"));
					//实例化商品类对象
					Category c = new Category(cname,cid);
					if (updcatservice.updCat(c)) {
						pw.write("1");
					}
				}
				
				
				
			}
			
			//关闭流
			pw.flush();
			pw.close();
		}
}
